import { type ResUserInfo } from "@/store/slice/userInfo";
import http, { AxiosRes, ResData } from "./http";
import { futimesSync } from "fs";

export type loginBody = {
	phone: string;
	code: string;
};

export type editUserInfoBody = {
	name: string; // 学生花名
	vChat: string; // 微信
	avatar?: string; // 头像，传上传之后的image地址 例如： xxxx.com/xxxx.png 看题目上传页有图片上传示意
	graduation_time: string; // 毕业时间
	money: number; // 目前薪资
};

export type subjectListBody = {
	title: string;
	value: string;
	children: subjectListBody[];
	can_exam: boolean;
};

export type examListBody = {
	created: Date;
	is_judge: boolean;
	subject_name: string;
	two_id: string;
	user_id: string;
	_id: string;
	topic_list: topic_list[];
};

export type topicBody = {
	created: Date;
	dec: string;
	title: string;
	two_id: string;
	_id: string;
	img: string[];
};

export type topic_list = {
	answer: string;
	created: Date;
	dec: string;
	title: string;
	two_id: string;
	_id: string;
	img: string[];
};

export function loginPost(value: loginBody) {
	return new Promise<ResUserInfo>(async (resolve, reject) => {
		let res: AxiosRes = await http.post("/api/user/login", value);
		resolve(res.data.data);
	});
}

export function editUserInfo(value: editUserInfoBody, userId: string) {
	return new Promise<ResData>(async (resolve, reject) => {
		let res: AxiosRes = await http.patch(`/api/user/${userId}`, value);
		resolve(res.data);
	});
}

export function getSubject() {
	return new Promise<subjectListBody[]>(async (resolve, reject) => {
		let res: AxiosRes<ResData<subjectListBody[]>> = await http.get(
			`/api/subject`
		);
		resolve(res.data.data);
	});
}

export function getExamHistory() {
	return new Promise<examListBody[]>(async (resolve, reject) => {
		let res: AxiosRes<ResData<examListBody[]>> = await http.post(`/api/exam`);
		resolve(res.data.data);
	});
}

export function getTopic(id: string) {
	return new Promise<topicBody[]>(async (resolve, reject) => {
		let res: AxiosRes<ResData<topicBody[]>> = await http.get(
			`/api/topic/${id}`
		);
		resolve(res.data.data);
	});
}
